package tk.mybatis.springboot.api.controller;


import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tk.mybatis.mapper.util.StringUtil;
import tk.mybatis.springboot.model.Message;
import tk.mybatis.springboot.ping.service.PingService;

@Controller
@RequestMapping("/ping")
public class PingController {
    @Autowired
    private PingService pingService;

    private static Logger LOGGER = LogManager.getLogger(PingController.class);

    @RequestMapping("webhoob")
    @ResponseBody
    public void web(@RequestHeader("X-Pingplusplus-Signature") String signature, HttpServletRequest request)
            throws Exception {
        pingService.web(signature, request);
    }

    @RequestMapping("pay")
    @ResponseBody
    public Message pay(String id, HttpServletRequest request, Integer type, String url, String usersCouponId) throws Exception {
        LOGGER.info("pay param type="+type+"    coupon="+usersCouponId+"    url="+url);
        return pingService.pay(id, request, type, url, StringUtil.isEmpty(usersCouponId) ? null : Integer.valueOf(usersCouponId));
    }
}
